package com.cgsd.heyu.ad;


import android.app.Activity;
import android.util.Log;

import com.android.library.utils.BeanUtils;
import com.apkfuns.logutils.LogUtils;
import com.bytedance.sdk.openadsdk.AdSlot;
import com.bytedance.sdk.openadsdk.TTAdConstant;
import com.bytedance.sdk.openadsdk.TTAdNative;
import com.bytedance.sdk.openadsdk.TTAdSdk;
import com.bytedance.sdk.openadsdk.TTRewardVideoAd;
import com.cgsd.heyu.api.AppConfig;
import com.cgsd.heyu.entity.AdContentEntity;
import com.qq.e.ads.rewardvideo.RewardVideoAD;
import com.qq.e.ads.rewardvideo.RewardVideoADListener;
import com.qq.e.comm.util.AdError;

import java.lang.ref.WeakReference;

/**
 * Description :  激励视频广告
 *
 * @author : xiaoxing
 * 时间：2020/3/13 09
 * 邮箱：only_dream136@sina.com
 */
public class AdRewardVideo {
    /**
     * 穿山甲相关
     */
    private TTRewardVideoAd mttRewardVideoAd;
    private TTAdNative mTTAdNative;
    /**
     * 广点通相关
     */
    private RewardVideoAD rewardVideoAD;

    private AdContentEntity adContentEntity;
    private WeakReference<Activity> weakReference;
    private AdListener adListener;


    public AdRewardVideo(Activity activity, AdContentEntity entity, AdListener adListener) {
        this.adListener = adListener;
        weakReference = new WeakReference<>(activity);
        this.adContentEntity = entity;

        if (BeanUtils.isEmpty(adContentEntity)) {
            return;
        }
        if (AdContentEntity.CHUANSHANJIA.equals(adContentEntity.platformType)) {
            mTTAdNative = TTAdSdk.getAdManager().createAdNative(weakReference.get());
        }

    }

    /**
     * 加载广告
     */
    public void loadAd() {
        if (AdContentEntity.GUANGDIANTONG.equals(adContentEntity.platformType)) {
            adRewardVideoG();
        } else {
            adRewardVideoC();
        }
    }

    /**
     * 加载广点通开屏甲短视频广搞
     */
    private void adRewardVideoG() {
        rewardVideoAD = new RewardVideoAD(weakReference.get(), adContentEntity.advertisingId, new RewardVideoADListener() {
            @Override
            public void onADLoad() {
                if (adListener != null) {
                    adListener.onSuccess();
                }
                rewardVideoAD.showAD(weakReference.get());
            }

            @Override
            public void onVideoCached() {

            }

            @Override
            public void onADShow() {

            }

            @Override
            public void onADExpose() {

            }

            @Override
            public void onReward() {

            }

            @Override
            public void onADClick() {

            }

            @Override
            public void onVideoComplete() {

            }

            @Override
            public void onADClose() {
                if (adListener != null) {
                    adListener.onVideoComplete();
                }
            }

            @Override
            public void onError(AdError adError) {
                LogUtils.e("onError addRewardVideo" + ",message=" + adError.getErrorMsg() + ",code=" + adError.getErrorCode());
                if (adListener != null) {
                    adListener.onFail();
                }
            }
        });
        rewardVideoAD.loadAD();
    }


    /**
     * 加载穿山开屏甲短视频广搞
     */
    private void adRewardVideoC() {
        AdSlot adSlot = new AdSlot.Builder()
                .setCodeId(adContentEntity.advertisingId)
                .setExpressViewAcceptedSize(500, 500)
                //必填参数，期望视频的播放方向：TTAdConstant.HORIZONTAL 或 TTAdConstant.VERTICAL
                .setOrientation(TTAdConstant.VERTICAL)
                .setUserID(AppConfig.AD_USER_ID)
                .build();

        mTTAdNative.loadRewardVideoAd(adSlot, new TTAdNative.RewardVideoAdListener() {
            @Override
            public void onError(int code, String message) {
                LogUtils.e("onError loadRewardVideoAd" + ",message=" + message + ",code=" + code);
                if (adListener != null) {
                    adListener.onFail();
                }
            }

            //视频广告加载后的视频文件资源缓存到本地的回调
            @Override
            public void onRewardVideoCached() {
                if (adListener != null) {
                    adListener.onSuccess();
                }
                mttRewardVideoAd.showRewardVideoAd(weakReference.get());
            }

            //视频广告素材加载到，如title,视频url等，不包括视频文件
            @Override
            public void onRewardVideoAdLoad(TTRewardVideoAd ad) {
                LogUtils.e("onRewardVideoAdLoad "+ad.toString());
                mttRewardVideoAd = ad;
                mttRewardVideoAd.setRewardAdInteractionListener(new TTRewardVideoAd.RewardAdInteractionListener() {

                    @Override
                    public void onAdShow() {
                    }

                    @Override
                    public void onAdVideoBarClick() {
                    }

                    @Override
                    public void onAdClose() {
                        if (adListener != null) {
                            adListener.onVideoComplete();
                        }
                    }

                    @Override
                    public void onVideoComplete() {

                    }

                    @Override
                    public void onVideoError() {

                    }

                    @Override
                    public void onRewardVerify(boolean b, int i, String s, int i1, String s1) {

                    }


                    @Override
                    public void onSkippedVideo() {

                    }
                });
            }
        });
    }


    /**
     * 清空数据
     */
    public void clear() {
        mTTAdNative = null;
        mttRewardVideoAd = null;
    }
}
